package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.IliquormallTopicMapper;
import org.linlinjava.litemall.db.domain.IliquormallLibrary;
import org.linlinjava.litemall.db.domain.IliquormallTopic;
import org.linlinjava.litemall.db.domain.IliquormallTopicExample;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ILiquormallTopicService {

    @Resource
    IliquormallTopicMapper liquorTopicMapper;

    public int insertLiquorTopic(IliquormallTopic liquorTopic) {
        liquorTopic.setAddTime(LocalDateTime.now());
        liquorTopic.setUpdateTime(LocalDateTime.now());
        return liquorTopicMapper.insert(liquorTopic);
    }

    public List<IliquormallTopic> queryLiquorTopicList(String title, Integer page, Integer limit, String sort, String order) {
        IliquormallTopicExample example = new IliquormallTopicExample();
        IliquormallTopicExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);

        return liquorTopicMapper.selectByExampleWithBLOBs(example);

    }

    public List<IliquormallTopic> queryLiquorTopicListByCategory(String title, Integer category_id, Integer page, Integer limit, String sort, String order) {
        IliquormallTopicExample example = new IliquormallTopicExample();
        IliquormallTopicExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }

        if (category_id != null){
            criteria.andCategoryIdEqualTo(category_id);
        }

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);

        return liquorTopicMapper.selectByExampleWithBLOBsWithOutContent(example);

    }

    public int updateLiquorTopic(IliquormallTopic liquorTopic) {
        liquorTopic.setUpdateTime(LocalDateTime.now());
        return liquorTopicMapper.updateByPrimaryKeyWithBLOBs(liquorTopic);
    }

    public int deleteLiquorTopic(Integer id) {
        return liquorTopicMapper.deleteByPrimaryKey(id);
    }

    public IliquormallTopic selectById(Integer id) {
        return liquorTopicMapper.selectByPrimaryKey(id);
    }

    public long count() {
        IliquormallTopicExample example = new IliquormallTopicExample();
        return liquorTopicMapper.countByExample(example);
    }

    public IliquormallTopic updateReadCount(IliquormallTopic liquorTopic) {
        String readCount = liquorTopic.getReadCount();
        if (readCount != "999+"){
            int rc = Integer.parseInt(readCount);
            rc = rc + 1;
            if (rc > 999){
                readCount = "999+";
            }else{
                readCount = String.valueOf(rc);
            }
            liquorTopic.setReadCount(readCount);
            liquorTopicMapper.updateByPrimaryKey(liquorTopic);
        }
        return liquorTopic;
    }

}
